<?php
include('header.php');

function getFileList($dir){
    $dir=iconv("utf-8","gb2312",$dir);
    if ($headle=opendir($dir)){
        while ($file=readdir($headle)){
            $file=iconv("gb2312","utf-8",$file);
            if ($file!='.' && $file!='..' ){
                $file = $dir."/".$file;
                global $list;
                if(is_file($file)){                   
                    $list[] = $file;
                }else{
                    getFileList($file);
                }
            }
        }
        closedir($headle);
    }
}
getFileList("./code");
foreach ($list as $k=>$vf){
    unset($dm);
    unset($insertData);
    unset($dataSet);
    unset($lines);
    unset($da);
    unset($name);
    $dm = substr($vf,-10,6);
    $db->where('dm',$dm);
    $db->where('version',0);
    if($db->getOne('complete')){ 
        //echo '<br>is you'; 
        continue;
    }
    $handle = @fopen($vf, "r"); 
    if ($handle) { 
       while (!feof($handle)) { 
           $lines[] = fgets($handle, 4096); 
       } 
       fclose($handle); 
    } 
    $da = explode(' ', $lines[0]);
    
    $keys = array('dm','name','data_time','jk','zg','zd','zxj','cjl','cje');
    $dm = $da[0];
    $name = '';
    foreach ($da as $k=>$v){
        if($k>0){
            $name .= iconv("gb2312","utf-8",$v);
        }
    }
    $name = str_replace('日线前复权', '', $name);
    echo '<br>'.iconv("utf-8","gb2312",$name);
    foreach ($lines as $k=>$v){
        if($k>1){
            $temp = explode(',', $v);
            $temp[0] = str_replace('/', '', $temp[0]);
            if(count($temp)<>7) {  continue;}
            array_unshift($temp, $dm,$name);
            //$dataSet[] = array('dm'=>$temp[0],'name'=>$temp[1],'data_time'=>$temp[2],'jk'=>$temp[3],'zg'=>$temp[4],'zd'=>$temp[5],'zxj'=>$temp[6],'cjl'=>$temp[7],'cje'=>$temp[8]);
            $dataSet[] = $temp;
            unset($temp);
        }
    }
    if(empty($dataSet)){ 
        $insertData = array('dm'=>$dm,'is_data'=>0);
        $db->insert('complete', $insertData);echo 'no data';
        continue;
    }
    if($db->insertMulti('all_record',$dataSet,$keys)){       
        $insertData = array('dm'=>$dm);
        $db->insert('complete', $insertData);
        unset($dm);
        unset($insertData);
        unset($dataSet);
        unset($lines);
        unset($da);
        unset($name);
    }else{
        var_dump($db->getLastError());
    }
}